import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, LabelEncoder
import joblib
from collections import defaultdict
import clean_data

def tes1_do():
    test = pd.read_csv('../data/test.csv')
    x_test = test[list(clean_data.Cleaning_data().keys())].copy()
    y_test = test['Attrition'].copy()

    # 加载转换器（注意路径是否正确！）
    woe_transforms = joblib.load('../model/transform/woe_transforms.pkl')
    label_encoders = joblib.load('../model/transform/label_encoders.pkl')

    # === 1. 应用 WOE（正确方式）===
    woe_features = ['Age', 'MonthlyIncome', 'TotalWorkingYears', 'DistanceFromHome']
    for col in woe_features:
        bins = woe_transforms[col]['bins']
        woe_map = woe_transforms[col]['woe_map']

        intervals = pd.cut(x_test[col], bins=bins, include_lowest=True)
        woe_vals = intervals.map(woe_map)  # Interval -> WOE
        x_test[f"{col}_WOE"] = pd.to_numeric(woe_vals, errors='coerce').fillna(0.0)

    # 2. 应用 LabelEncoder
    cat_cols = [
        'BusinessTravel', 'Education', 'JobRole',
        'MaritalStatus', 'OverTime', 'EducationField', 'Department'
    ]
    for col in cat_cols:
        le = label_encoders[col]
        # 处理未见过的类别
        x_test[col] = x_test[col].apply(lambda val: val if val in le.classes_ else '<UNK>')
        # 如果 '<UNK>' 不在 classes_ 中，临时添加
        if '<UNK>' not in le.classes_:
            le.classes_ = np.append(le.classes_, '<UNK>')
        x_test[col] = le.transform(x_test[col])

    return x_test, y_test